#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>
#include <fcntl.h>

#include <pthread.h>
#include "hzqSocket.h"
#include "hzqFormat.h"

short deviceIp = -1;
short host2Ip = -1;

int tids_num = 8;

void *routine(void *arg);
int main(int argc,char *argv[])
{
    int i=1;

    if(argc<3){
        printf("%s [host2 port] [device port]\n",argv[0]);
        exit(0);
    }else{
        host2Ip  = atoi(argv[1]);
        deviceIp = atoi(argv[2]);
    }
    
    //开8个线程同时给device和host2发送图片
    pthread_t tids[tids_num];
    for(i=1; i <= tids_num; i++){
        if(pthread_create(&(tids[i-1]), NULL,routine, (void *)(ulong)i) != 0){
			perror("create threads error");
		}
	}
    //代码中如果没有pthread_join主线程会很快结束从而使整个进程结束
    for(i=1;i<=tids_num;i++){
        pthread_join(tids[i-1],NULL);
    }
    //routine((void *)(ulong)1);

    return 0;
}

/**
 *  @brief  线程回调函数，这里的给device和host2发送./result/img/目录下的图片文件
 *  @param  arg: ./result/img/目录下的第 arg 张jpg图片 
 *  @retval NULL
 */
void *routine(void *arg)
{
    int fd = -1;
    unsigned char send_cmd[maxMessageSize];

    char imgName[maxMessageSize];
    snprintf((char *)imgName,maxMessageSize,"%s","./result/img/0.jpg");//构造文件名
    imgName[13] = '0' + (ulong)arg;


    /* 1.尝试打开文件 */
    //printf("try to send file: %s\n",imgName);
    fd=open(imgName,O_RDONLY);
	if(fd == -1) {
        perror("host1-> when open");
        return NULL;
	}
    int devicefd = connect_serv("127.0.0.1",deviceIp);      //使用短连接
    int host2fd  = connect_serv("127.0.0.1",host2Ip);
    assert(devicefd != -1);
    assert(host2fd != -1);

    /* 2.发送文件名 */
    bzero(send_cmd,maxMessageSize);
    snprintf((char *)send_cmd+6,maxMessageSize-6,"%s%ld%s","./result/receiveImgFromDevice/",(ulong)arg,".jpg");
    package_head(send_cmd,e_msgSendFile,strlen((const char *)send_cmd+6)+1);
    Write(devicefd,send_cmd,strlen((const char *)send_cmd+6)+1+6);
    printf("try to send file: %s to %d\n",send_cmd+6,devicefd);

    bzero(send_cmd,maxMessageSize);
    snprintf((char *)send_cmd+6,maxMessageSize-6,"%s%ld%s","./result/receiveImgFromHost1/",(ulong)arg,".jpg");
    package_head(send_cmd,e_msgSendFile,strlen((const char *)send_cmd+6)+1);
    Write(host2fd,send_cmd,strlen((const char *)send_cmd+6)+1+6);
    printf("try to send file: %s to %d\n",send_cmd+6,host2fd);

    /* 3.发送文件内容 */
    while(1){
        bzero(send_cmd,maxMessageSize);
        int r=read(fd,send_cmd+6,maxMessageSize-6);
        if(r<=0){//读完了,就结束文件传输
            bzero(send_cmd,maxMessageSize);
            package_head(send_cmd,e_msgEndFile,0);
            Write(devicefd,send_cmd,6);
            Write(host2fd,send_cmd,6);
            printf("send completed，not mean success\n");
            break;
        }
        //printf("r=%d\n",r);
        package_head(send_cmd,e_msgFileContent,r);
        Write(devicefd,send_cmd,6+r);
        Write(host2fd,send_cmd,6+r);
    }

    /* 4.释放文件句柄 */
    close(fd);

    int flags = 0;
    flags = fcntl(host2fd,F_GETFL,0);         
    fcntl(host2fd,F_SETFL,flags&~O_NONBLOCK);
    if(Read(host2fd,send_cmd,6)>0){             //阻塞到对端接收完成并返回
        printf("%s send to hodevice success\n",imgName);
    }

    flags = fcntl(devicefd,F_GETFL,0);
    fcntl(devicefd,F_SETFL,flags&~O_NONBLOCK);
    if(Read(devicefd,send_cmd,6)>0){
        printf("%s send to hodevice success\n",imgName);
    }
    printf("end???");
    close(devicefd);
    close(host2fd);

	return NULL;
}

